<template>
  <el-dialog :title="title" :visible.sync="dialogShow" width="40%" @close="closeDialog">
    <!--绘制表单-->
    <el-form ref="form" :model="formData" :rules="formRules" label-width="180px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:440px"/>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:440px"/>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width:80%"
          placeholder="请选择"
          @focus="getManagerList"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门描述" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" style="width:440px" rows="3"/>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="submitDept">确定</el-button>
        <el-button @click="dialogShow=false">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addDepartmentApi, editDepartmentApi, getDepartmentInfoApi, getDepartmentListApi } from '@/api/department'
import { getEmployeeListApi } from '@/api/employees'

export default {
  name: 'AddDept',
  data() {
    // 部门名称查重校验
    const checkNameRepeat = async(valid, value, callback) => {
      // 1. 找到所有的部门名称
      const { depts } = await getDepartmentListApi()
      // console.log(depts)
      // 2. 找到当前部门的兄弟部门
      let arr = depts.filter(item => item.pid === this.formData.pid)
      // 2.5 如果是修改部门, 需要把自己排除在外
      arr = arr.filter(item => item.id !== this.formData.id)
      // 3. 名称查重
      arr.some(item => item.name === value) ? callback(new Error('部门名称重复')) : callback()
    }
    // 部门编码查重校验
    const checkCodeRepeat = async(valid, value, callback) => {
      // 1. 找到所有的部门编码
      let { depts } = await getDepartmentListApi()
      // 1.5 如果是修改部门, 需要把自己排除在外
      if (this.formData.id) {
        depts = depts.filter(item => item.id !== this.formData.id)
      }
      // 2. 直接编码查重
      const isRepeat = depts.some(item => item.code === value)
      // console.log(isRepeat)
      isRepeat ? callback(new Error('部门编码重复')) : callback()
    }
    return {
      dialogShow: false,
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      formRules: {
        code: [
          { required: true, message: '请输入部门编号', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ]
      },
      managerList: []
    }
  },
  computed: {
    // 通过路由参数判断是新增还是修改
    title() {
      return this.formData.id ? '修改部门' : '新增部门'
    }
  },
  methods: {
    // 获取负责人数据
    async getManagerList() {
      this.managerList = await getEmployeeListApi()
    },
    // 提交事件逻辑
    async submitDept() {
      // 1. 表单校验
      try {
        await this.$refs.form.validate()
      } catch (e) {
        return console.log('校验未通过')
      }
      // 2. 提交表单
      // console.log(this.formData)
      // 3. 提示成功
      if (this.formData.id) {
        await editDepartmentApi(this.formData)
        this.$message.success('修改部门成功')
      } else {
        await addDepartmentApi(this.formData)
        this.$message.success('新增部门成功')
      }
      // 4. 更新列表
      this.$emit('updateList')
      // 5. 关闭并重置表单
      this.dialogShow = false
    },
    // 关闭弹框
    closeDialog() {
      console.log('关闭弹框')
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
      this.$refs.form.clearValidate()
    },
    // 获取回显的数据
    async getFormDataInfo(id) {
      this.formData = await getDepartmentInfoApi(id)
      this.dialogShow = true
    }
  }
}
</script>

<style scoped>

</style>
